package cn.fastoo.common.service.task;
import cn.fastoo.common.dao.member.MemberDao;
import cn.fastoo.common.dao.member.MemberGradeDao;
import cn.fastoo.common.dao.system.AdminSystemConfigDao;
import cn.fastoo.common.dao.system.AdminSystemMsgDao;
import cn.fastoo.common.entity.base.BaseMeasurement;
import cn.fastoo.common.entity.member.Member;
import cn.fastoo.common.entity.member.MemberGrade;
import cn.fastoo.common.entity.system.AdminSystemMsg;
import cn.fastoo.common.sampleframe.dao.FastDao;
import cn.fastoo.common.sampleframe.dao.impl.FastDaoImpl;
import cn.fastoo.common.sampleframe.search.Search;
import cn.fastoo.common.sampleframe.springside.Collections3;
import cn.fastoo.common.sampleframe.utils.DateUtils;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Component
public class CommonTaskService{

    @Autowired
    private MemberGradeDao memberGradeDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private AdminSystemConfigDao systemConfigDao;
    @Autowired
    private AdminSystemMsgDao adminSystemMsgDao;



    @Scheduled(cron = "0/15 * * * * ?")
    private void shifang(){
        //所有非正式会员等级的会员
        List<MemberGrade> memberGrades = memberGradeDao.findAllOffical();
        List<String> ids = new ArrayList<>();
        if(Collections3.isEmpty(memberGrades)){
            return ;

        }
        for (MemberGrade memberGrade : memberGrades) {
            ids.add(memberGrade.getId().toString());
        }

        /**
         * 释放逻辑开始
         */
        String shifangDay = systemConfigDao.getValue(AdminSystemConfigDao.Name.释放天数);
        if(StringUtils.isNotBlank(shifangDay)){
            //根据收集时间 来判断
            //加入时间判断.
            //当前时间-释放天数<收集时间
            try {
                Date date = DateUtils.addDays(new Date(),-Integer.valueOf(shifangDay));
                List<Member> members = memberDao.find(new Search()
                        .addSearchFilter("NOTIN_memberGradeId",ids)
                        .addSearchFilter("LT_collectTime",date)
                        .addSearchFilter("NEQ_salesUserId",0));
                if(Collections3.isNotEmpty(members)){
                    memberDao.shifang(members);
                    adminSystemMsgDao.shifang(members);
                }
            }catch (Exception e){
                e.printStackTrace();
                return;
            }
        }

        /**
         * 释放逻辑结束
         */
        /**
         * 预警逻辑开始
         */
        String yujingDay = systemConfigDao.getValue(AdminSystemConfigDao.Name.预警天数);
        if(StringUtils.isNotBlank(yujingDay)){
            //根据收集时间 来判断
            //加入时间判断.
            //当前时间-预警天数<收集时间
            try {
                Date date = DateUtils.addDays(new Date(),-Integer.valueOf(yujingDay));
                List<Member> members = memberDao.find(new Search()
                        .addSearchFilter("NOTIN_memberGradeId",ids)
                        .addSearchFilter("LT_collectTime",date)
                        .addSearchFilter("NEQ_salesUserId",0)
                        .addSearchFilter("EQ_yujingFlag",0));
                if(Collections3.isNotEmpty(members)){
                    memberDao.updateYujing(members);
                    adminSystemMsgDao.yujing(members,Integer.valueOf(shifangDay));
                }
            }catch (Exception e){
                e.printStackTrace();
                return;
            }
        }
        /**
         * 预警逻辑结束
         */
    }


}
